﻿using JavaDevToys.Rcl.Models;
using Microsoft.VisualBasic;
using NPinyin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace JavaDevToys.Rcl.Utils
{
    public static class JavaCodeHelper
    {
        public static string ToCamelCase(string str)
        {
            if (string.IsNullOrEmpty(str))
                return str;
            string str1 = string.Empty;
            var strings = str.Split('_');
            foreach (var item in strings)
            {
                str1 += char.ToUpper(item[0]) + item.Substring(1).ToLower();
            }
            return char.ToLower(str1[0]) + str1.Substring(1);
        }

        // 转换为Pascal命名法
        public static string ToPascalCase(string name)
        {
            if (string.IsNullOrEmpty(name)) return name;

            string camel = ToCamelCase(name);
            return char.ToUpper(camel[0]) + camel.Substring(1);
        }
        /// <summary>
        /// 首字母大写
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string Toupperfirst(string name)
        {
            return ToUpperFirst(name);
        }
        public static string ToUpperFirst(string name)
        {
            if (string.IsNullOrEmpty(name)) return name;
            return char.ToUpper(name[0]) + name.Substring(1);
        }
        /// <summary>
        /// 首字母小写
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string Tolowerfirst(string name)
        {
            return ToLowerFirst(name);
        }
        public static string ToLowerFirst(string name)
        {
            if (string.IsNullOrEmpty(name)) return name;
            return char.ToLower(name[0]) + name.Substring(1);
        }

        public static string Tolower(string name)
        {
            return name.ToLower();
        }

        public static string Toupper(string name)
        {
            return name.ToUpper();
        }

        public static string GetPinyinInitials(string input)
        {
            return Pinyin.GetInitials(input).ToUpper();
        }

        public static string get_pinyin_initials(string input)
        {
            return GetPinyinInitials(input);
        }

        public static string Replace(string input, string oldValue, string newValue)
        {
            return input?.Replace(oldValue, newValue);
        }

        public static bool contains(string name,string value)
        {
            if(name==null)
                name=string.Empty;
            return name.Contains(value, StringComparison.OrdinalIgnoreCase);
        }

        public static string get_validation_annotation(JavaTempData_Parameter javaTemp)
        {
            string annotation = string.Empty;
            switch (javaTemp.data_type.ToLower())
            {
                case "string":
                    annotation = "@NotBlank(message=\"" + javaTemp.description + "不能为空\")";
                    break;
                case "integer":
                case "long":
                case "double":
                case "float":
                case "boolean":
                    annotation = "@NotNull(message=\"" + javaTemp.description + "不能为空\")";
                    break;
                default:
                    if (is_list_type(javaTemp.data_type))
                    {
                        annotation = "@NotEmpty(message=\"" + javaTemp.description + "不能为空\")";
                    }
                    else
                    {
                        annotation = "@NotNull(message=\"" + javaTemp.description + "不能为空\")";
                    }
                    break;
            }

            return annotation;
        }

        public static string map_data_type(string dataType, string parameterName)
        {
            // 映射数据类型，可根据需要扩展
            switch (dataType.ToLower())
            {
                case "string":
                    return "String";
                case "integer":
                    return "Integer";
                case "long":
                    return "Long";
                case "double":
                    return "Double";
                case "float":
                    return "Float";
                case "datetime":
                    return "Date";
                case "time":
                    return "Date";
                case "boolean":
                    return "Boolean";
                case "list<Class>":
                    return $"List<{parameterName.Replace("List", "DTO")}>";
                default:
                    // 如果遇到未知类型，直接返回原始类型
                    return dataType;
            }
        }

        public static bool is_list_type(string dataType)
        {
            return dataType.Trim().StartsWith("List<", StringComparison.OrdinalIgnoreCase);
        }
    }
}
